package com.jxdinfo.hussar.example.outsourcereportandcertimanage.bggl.wsdexttestingreport1.service.impl;

import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.platform.core.utils.HussarUtils;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bggl.wsdexttestingreport1.model.WsdExttestingReport1;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bggl.wsdexttestingreport1.service.WsdExttestingReport1Service;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bggl.wsdexttestingreport1.dao.WsdExttestingReport1Mapper;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bggl.wsdexttestingreport1.dto.WsdExttestingReport1SelectCondition;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import com.jxdinfo.hussar.common.tfmodel.TfModelEnum;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bggl.wsdexttestingreport1.dao.WsdExttestingReport1Mapper;
import com.jxdinfo.hussar.common.security.BaseSecurityUtil;
import com.jxdinfo.hussar.support.transaction.core.annotation.HussarTransactional;
import com.jxdinfo.hussar.support.mp.base.query.generator.SingleTableQueryGenerator;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import com.jxdinfo.hussar.support.mp.base.query.generator.superquery.SuperQueryGenerator;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import java.util.ArrayList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bggl.wsdexttestingreport1.vo.WsdExttestingReport1PageVO;
import java.util.Map;
import com.jxdinfo.hussar.support.mp.base.query.dto.QueryConditionDto;
import com.jxdinfo.hussar.support.mp.base.query.dto.SuperQueryConditionDto;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 委外报告管理1服务实现类
 */
@Service("outsourcereportandcertimanage.bggl.wsdexttestingreport1.WsdExttestingReport1ServiceImpl")
@HussarDs("master")
public class WsdExttestingReport1ServiceImpl extends HussarServiceImpl<WsdExttestingReport1Mapper, WsdExttestingReport1> implements WsdExttestingReport1Service {

    private static final Logger logger = LoggerFactory.getLogger(WsdExttestingReport1ServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private WsdExttestingReport1Mapper wsdExttestingReport1Mapper;

    @Override
    public ApiResponse<WsdExttestingReport1PageVO> ConditionFilterPage(WsdExttestingReport1SelectCondition wsdExttestingReport1SelectCondition) {
        try {
            Page<WsdExttestingReport1> pager = new Page<>(wsdExttestingReport1SelectCondition.getCurrent(), wsdExttestingReport1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdExttestingReport1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdExttestingReport1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdExttestingReport1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdExttestingReport1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdExttestingReport1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdExttestingReport1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdExttestingReport1> superQueryGenerator = new SuperQueryGenerator(WsdExttestingReport1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdExttestingReport1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdExttestingReport1::getDelFlag, "1").or().isNull(WsdExttestingReport1::getDelFlag)));
            WsdExttestingReport1PageVO wsdExttestingReport1PageVO = new WsdExttestingReport1PageVO();
            wsdExttestingReport1PageVO.setData(list.getRecords());
            wsdExttestingReport1PageVO.setCount(list.getTotal());
            wsdExttestingReport1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdExttestingReport1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdExttestingReport1PageVO> ConditionFilterPage_order_custom(WsdExttestingReport1SelectCondition wsdExttestingReport1SelectCondition) {
        try {
            Page<WsdExttestingReport1> pager = new Page<>(wsdExttestingReport1SelectCondition.getCurrent(), wsdExttestingReport1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdExttestingReport1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdExttestingReport1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdExttestingReport1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdExttestingReport1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdExttestingReport1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdExttestingReport1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdExttestingReport1> superQueryGenerator = new SuperQueryGenerator(WsdExttestingReport1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdExttestingReport1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdExttestingReport1::getDelFlag, "1").or().isNull(WsdExttestingReport1::getDelFlag)));
            WsdExttestingReport1PageVO wsdExttestingReport1PageVO = new WsdExttestingReport1PageVO();
            wsdExttestingReport1PageVO.setData(list.getRecords());
            wsdExttestingReport1PageVO.setCount(list.getTotal());
            wsdExttestingReport1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdExttestingReport1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<Boolean> flagDelete(List<String> ids) {
        try {
            return ApiResponse.success(wsdExttestingReport1Mapper.flagDelete(ids));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_DELETION_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<Boolean> del(List<String> ids) {
        try {
            return ApiResponse.success(this.removeByIds(ids));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_DELETION_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> insertOrUpdate(WsdExttestingReport1 wsdExttestingReport1) {
        try {
            if (wsdExttestingReport1.getBusinessId() == null || this.getById(wsdExttestingReport1.getBusinessId()) == null) {
                wsdExttestingReport1.setCreator(BaseSecurityUtil.getUser().getId());
                wsdExttestingReport1.setCreateTime(LocalDateTime.now());
                wsdExttestingReport1.setDelFlag("0");
                wsdExttestingReport1.setDataOrgan(BaseSecurityUtil.getUser().getDeptId());
            }
            this.saveOrUpdate(wsdExttestingReport1);
            return ApiResponse.success(String.valueOf(wsdExttestingReport1.getBusinessId()), "");
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FAILED_T0_ADD_OR_MODIFY.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdExttestingReport1> formQuery(String id) {
        try {
            return ApiResponse.success(this.getById(id));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdExttestingReport1PageVO> ConditionFilter(WsdExttestingReport1SelectCondition wsdExttestingReport1SelectCondition) {
        try {
            QueryWrapper<WsdExttestingReport1> queryWrapper = Wrappers.query();
            if (HussarUtils.isNotEmpty(wsdExttestingReport1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdExttestingReport1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdExttestingReport1> superQueryGenerator = new SuperQueryGenerator(WsdExttestingReport1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            WsdExttestingReport1PageVO wsdExttestingReport1PageVO = new WsdExttestingReport1PageVO();
            List<WsdExttestingReport1> list = this.list(queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdExttestingReport1::getDelFlag, "1").or().isNull(WsdExttestingReport1::getDelFlag)));
            wsdExttestingReport1PageVO.setData(list);
            if (HussarUtils.isNotEmpty(list)) {
                wsdExttestingReport1PageVO.setCount((long) list.size());
            }
            wsdExttestingReport1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdExttestingReport1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdExttestingReport1PageVO> ConditionFilterwsdExttestingReport1Sort_1Page(WsdExttestingReport1SelectCondition wsdExttestingReport1SelectCondition) {
        try {
            Page<WsdExttestingReport1> pager = new Page<>(wsdExttestingReport1SelectCondition.getCurrent(), wsdExttestingReport1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdExttestingReport1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdExttestingReport1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            String[] defaultOrderParameter = new String[1];
            String defaultOrder = "";
            defaultOrder += "lastModifiedTime" + ",desc;";
            defaultOrderParameter[0] = defaultOrder;
            parameterMap.put("defaultOrderRule", defaultOrderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdExttestingReport1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdExttestingReport1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdExttestingReport1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdExttestingReport1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdExttestingReport1> superQueryGenerator = new SuperQueryGenerator(WsdExttestingReport1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdExttestingReport1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdExttestingReport1::getDelFlag, "1").or().isNull(WsdExttestingReport1::getDelFlag)));
            WsdExttestingReport1PageVO wsdExttestingReport1PageVO = new WsdExttestingReport1PageVO();
            wsdExttestingReport1PageVO.setData(list.getRecords());
            wsdExttestingReport1PageVO.setCount(list.getTotal());
            wsdExttestingReport1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdExttestingReport1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdExttestingReport1PageVO> ConditionFilterwsdExttestingReport1Sort_1Page_order_custom(WsdExttestingReport1SelectCondition wsdExttestingReport1SelectCondition) {
        try {
            Page<WsdExttestingReport1> pager = new Page<>(wsdExttestingReport1SelectCondition.getCurrent(), wsdExttestingReport1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdExttestingReport1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdExttestingReport1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            String[] defaultOrderParameter = new String[1];
            String defaultOrder = "";
            defaultOrder += "lastModifiedTime" + ",desc;";
            defaultOrderParameter[0] = defaultOrder;
            parameterMap.put("defaultOrderRule", defaultOrderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdExttestingReport1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdExttestingReport1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdExttestingReport1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdExttestingReport1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdExttestingReport1> superQueryGenerator = new SuperQueryGenerator(WsdExttestingReport1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdExttestingReport1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdExttestingReport1::getDelFlag, "1").or().isNull(WsdExttestingReport1::getDelFlag)));
            WsdExttestingReport1PageVO wsdExttestingReport1PageVO = new WsdExttestingReport1PageVO();
            wsdExttestingReport1PageVO.setData(list.getRecords());
            wsdExttestingReport1PageVO.setCount(list.getTotal());
            wsdExttestingReport1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdExttestingReport1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }
}
